class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        if pHead1 is None or pHead2 is None:
            return None
        stack1 = []
        stack2 = []
        while pHead1 is not None:
            stack1.append(pHead1)
            pHead1 = pHead1.next
        while pHead2 is not None:
            stack2.append(pHead2)
            pHead2 = pHead2.next
        pre_node = None
        pop1 = stack1.pop()
        pop2 = stack2.pop()
        while pop1 is pop2 and len(stack1) == 0 and len(stack2) == 0:
            pre_node = pop1
            pop1 = stack1.pop()
            pop2 = stack2.pop()
        if len(stack1) != 0 or len(stack2) != 0:
            return pop1
        return pre_node
